﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Test.Repository.Interfaces;
using Test.Models.Base;
using Test.Models;
using Test.Models.Interfaces;
using Test.Repository.Base;
using MvcMiniProfiler;
using System.Data.Entity;

namespace Test.Repository
{
    public class Elements : RepositoryBase, IElements
    {
        public Elements(DB _db) : base(_db) { }

        public Element Find(int elementID)
        {
            var profiler = MiniProfiler.Current;

            using (profiler.Step("Repository Elements Find"))
            {
                return db.Elements.Find(elementID);
            }
        }

        public IEnumerable<Element> GetAll()
        {
            var profiler = MiniProfiler.Current;

            IQueryable<Element> elements;
            List<Element> list;

            using (profiler.Step("Repository Elements GetAll"))
            {
                elements = db.Elements
                                .Where(e => e.visible &&
                                            e.ElementLanguages.Any())
                                .OrderByDescending(e => e.created);
            }

            using (profiler.Step("Repository Elements -> To list"))
            {
                list = elements.ToList();
            }

            using (profiler.Step("Repository Elements Return"))
            {
                return list;
            }
            
        }
    }
}